package com.xiaoye.qbproject.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson2.JSONArray;
import com.xiaoye.qbproject.domain.wiki.Reference;
import com.xiaoye.qbproject.util.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.util.List;
@MappedTypes(List.class)  // 处理 List 类型
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ListRefToJsonConverter implements Converter<List<Reference>> {
    @Override
    public List<Reference> convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue = cellData.getStringValue();
        if (StringUtils.isBlank(stringValue)) {
            return null;
        }
        return JSONArray.parseArray(stringValue, Reference.class);
    }

    @Override
    public WriteCellData<?> convertToExcelData(List<Reference> value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return null;
        }
        return new WriteCellData<>(JSONArray.toJSONString(value));
    }
}
